Skip to content

Add LambdaInitialData<T> + AddInitialData lambda overload (8.14.0)#250

Merged
jeremydmiller merged 1 commit intomasterfrom
feature/lambda-initial-data-2539
Apr 20, 2026
Merged

Add LambdaInitialData<T> + AddInitialData lambda overload (8.14.0)#250
jeremydmiller merged 1 commit intomasterfrom
feature/lambda-initial-data-2539

Conversation

@jeremydmiller
Copy link
Copy Markdown
Member

Summary

Lets callers register small inline seeders without authoring a dedicated IInitialData<TContext> class:

services.AddInitialData<ShopDbContext>(async (ctx, ct) =>
{
    ctx.Customers.Add(new ShopCustomer { Name = "Inline" });
    await ctx.SaveChangesAsync(ct);
});

Registered as a singleton LambdaInitialData<T> so it coexists with the existing class-based AddInitialData<TContext, TData>() form and runs in registration order via DatabaseCleaner<T>.ResetAllDataAsync.

Background

Needed by Wolverine GH-2539 — part of the "EF Core dev-time improvements" bullet list. Wolverine will consume the new API in its dev-time documentation and reset-between-tests story. Package bumped to 8.14.0 so Wolverine can pull the new version.

Changes

File Change
src/Weasel.EntityFrameworkCore/LambdaInitialData.cs New sealed IInitialData<TContext> implementation wrapping a Func<TContext, CancellationToken, Task>
src/Weasel.EntityFrameworkCore/DatabaseCleanerExtensions.cs New AddInitialData<TContext>(Func<TContext, CancellationToken, Task>) overload registered as singleton
src/Weasel.EntityFrameworkCore.Tests/Postgresql/lambda_initial_data_tests.cs 3 new tests: DI resolution, class + lambda coexistence through a full reset, null-delegate guard
src/Weasel.EntityFrameworkCore.Tests/Postgresql/database_cleaner_tests.cs [Collection("FkDependencyDbContext")] so it serializes with the new suite (both share the fk_dep_test schema)
src/DocSamples/DatabaseCleanerSamples.cs New sample_efcore_lambda_initial_data region
docs/efcore/database-cleaner.md New "Inline lambda seeders" subsection under IInitialData
Directory.Build.props 8.13.0 → 8.14.0

Test plan

  • dotnet test --framework net9.0 --filter "FullyQualifiedName~lambda_initial_data|FullyQualifiedName~database_cleaner"7/7 pass (3 new + 4 existing cleaner tests)
  • dotnet build — 0 errors
  • CI across net8 / net9 / net10

🤖 Generated with Claude Code

Lets callers register small inline seeders without authoring a dedicated
IInitialData<TContext> class:

    services.AddInitialData<ShopDbContext>(async (ctx, ct) =>
    {
        ctx.Customers.Add(new ShopCustomer { Name = "Inline" });
        await ctx.SaveChangesAsync(ct);
    });

Registered as a singleton LambdaInitialData<T> so it coexists with the
existing class-based AddInitialData<TContext, TData>() form and runs in
registration order via the DatabaseCleaner<T>.ResetAllDataAsync path.

Also adds a [Collection("FkDependencyDbContext")] guard to
database_cleaner_tests so it serializes with the new
lambda_initial_data_tests (both share the fk_dep_test schema).

Bumps Directory.Build.props Version to 8.14.0 for the Wolverine consumer
that depends on this work (JasperFx/wolverine#2539).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jeremydmiller jeremydmiller merged commit 07d6880 into master Apr 20, 2026
21 checks passed
@jeremydmiller jeremydmiller deleted the feature/lambda-initial-data-2539 branch April 20, 2026 17:29
erikshafer pushed a commit to erikshafer/wolverine that referenced this pull request Apr 20, 2026
Makes the EF Core dev-loop faster and more discoverable:

- Auto-register Weasel's DatabaseCleaner<> (open generic) inside
  UseEntityFrameworkCoreTransactions(). Callers no longer need a
  per-context services.AddDatabaseCleaner<T>() — resolving
  IDatabaseCleaner<T> / DatabaseCleaner<T> just works.

- New IHost.ResetAllDataAsync<T>() extension for finer-grained
  per-DbContext test teardown. Creates its own scope, resolves the
  DbContext first, then the cleaner, then runs delete+reseed.

- New documentation:
  * docs/guide/durability/efcore/index.md — Development-time usage
    section covering Weasel migrations vs EF Core migrations,
    IInitialData mention, and reset guidance.
  * docs/guide/durability/efcore/initial-data.md — IInitialData<T>
    patterns: class-based vs lambda, layered seeders, idempotency,
    when not to use.
  * docs/guide/durability/efcore/batch-queries.md — four handler
    patterns for the Wolverine batching story, with locally measured
    perf numbers (2.78x speedup, 4-query handler) and links out to
    Weasel's fluent BatchedQuery reference.

- Weasel link sweep: replaced github.com links with
  https://weasel.jasperfx.net/... where applicable.

- ItemService sample: SeedSampleItems IInitialData<ItemsDbContext>
  registered via AddInitialData<TContext, TData>(), with a comment
  showing the Weasel 8.14 lambda overload.

- Bumps Weasel.* package references to 8.14.0 to pick up the new
  LambdaInitialData<T> + AddInitialData lambda overload
  (JasperFx/weasel#250).

Tests: 7/7 pass for the cleaner-related suite
(auto_database_cleaner_registration_tests + database_cleaner_usage_tests).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant